package com.quwancode.springbootmysql.controller;


import com.quwancode.springbootmysql.common.page.PageResult;
import com.quwancode.springbootmysql.common.utils.Result;
import com.quwancode.springbootmysql.dto.StudentCreateDto;
import com.quwancode.springbootmysql.dto.StudentUpdateDto;
import com.quwancode.springbootmysql.query.StudentQuery;
import com.quwancode.springbootmysql.service.StudentService;
import com.quwancode.springbootmysql.vo.StudentVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chuck
 * @since 2023-12-09
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<StudentVo>> page(@Valid StudentQuery query) {
        PageResult<StudentVo> page = studentService.page(query);

        return Result.ok(page);
    }

    @GetMapping("/{id}")
    @Operation(summary = "信息")
    public Result<StudentVo> get(@PathVariable("id") Long id) {

        return Result.ok(studentService.get(id));
    }

    @PostMapping()
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody StudentCreateDto dto) {
        studentService.save(dto);

        return Result.ok();
    }

    @PutMapping()
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody @Valid StudentUpdateDto dto) {
        studentService.update(dto);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList) {
        studentService.delete(idList);

        return Result.ok();
    }

}


